/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package es.aurea.presentation.security;

import es.aurea.presentation.utils.EJBLocator;
import es.aurea.transferobjects.UserTO;
import java.io.Serializable;
import java.util.logging.Logger;
import javax.faces.context.FacesContext;

/**
 *
 * @author zarius
 */
public class Security implements Serializable {

    private Logger logger = Logger.getLogger(this.getClass().getName());
    private UserTO user = null;

    private void loadUser() {
        if (getIsUserLogued() && user == null) {
            UserTO newUser = new UserTO(FacesContext.getCurrentInstance().getExternalContext().getRemoteUser());
            user = EJBLocator.getInstance().getSecurityBean().find(newUser);
        } 
    }

    public String getUserName() {
        loadUser();
        return user.getUsername();
    }

    /**
     * @return the isUserLogued
     */
    public boolean getIsUserLogued() {
        if (FacesContext.getCurrentInstance() != null && FacesContext.getCurrentInstance().getExternalContext() != null && FacesContext.getCurrentInstance().getExternalContext().getRemoteUser() != null) {
            return true;
        } else {
            return false;
        }
    }

    public UserTO getUser() {
        return user;
    }
}
